package tools;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

public class TwittParser {

	/**
	 * pour chaque categorie, la liste de tous les twitts (apprentissage)
	 */
	private HashMap<String, ArrayList<String>> corpus;
	/**
	 * liste des twitts (test)
	 */
	private ArrayList<String> document;

	/**
	 * Constructeur d'apprentissage
	 * 
	 * @param trainerPath
	 *            chemin du fichier d'apprentissage
	 * @param includeFirstLine
	 *            validite de la premiere ligne (false si les titres des
	 *            colonnes sont écrits au debut)
	 * @param twittColumn
	 *            numero de la colonne qui contient les twitts
	 * @param categoryColumn
	 *            numero de la colonne qui contient les categories
	 */
	private TwittParser(String trainerPath, boolean includeFirstLine,
			int twittColumn, int categoryColumn) {

		corpus = new HashMap<String, ArrayList<String>>();

		try {
			FileInputStream fstream = new FileInputStream(trainerPath);

			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			// jump first line if need be
			if (!includeFirstLine) {
				br.readLine();
			}
			// Read File Line By Line and all all the words
			while ((strLine = br.readLine()) != null) {
				treatTwitt(strLine, twittColumn, categoryColumn);
			}
			// Close the input stream
			in.close();

		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}

	}

	/**
	 * Constructeur de test
	 * 
	 * @param trainerPath
	 *            chemin du fichier de test
	 * @param includeFirstLine
	 *            validite de la premiere ligne (false si les titres des
	 *            colonnes sont écrits au debut)
	 * @param twittColumn
	 *            numero de la colonne qui contient les twitts
	 */
	public TwittParser(String trainerPath, boolean includeFirstLine,
			int twittColumn) {
		document = new ArrayList<String>();
		try {
			FileInputStream fstream = new FileInputStream(trainerPath);

			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			// jump first line if need be
			if (!includeFirstLine) {
				br.readLine();
			}

			// Read File Line By Line and all all the words
			while ((strLine = br.readLine()) != null) {
				treatTwitt(strLine, twittColumn);
			}
			// Close the input stream
			in.close();

		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	private void treatTwitt(String strLine, int twittColumn, int categoryColumn) {
		
		String[] lis = strLine.split(",");
		if (lis.length <= categoryColumn) {
			throw new ArrayIndexOutOfBoundsException(strLine);
		} else if (lis[categoryColumn].length() < 1) {
			throw new ArrayIndexOutOfBoundsException(lis[categoryColumn]);
		}
		
		String cat = lis[categoryColumn].substring(1,
				lis[categoryColumn].length() - 1);
		String twit = lis[twittColumn].substring(1,
				lis[twittColumn].length() - 1);

		if (!corpus.containsKey(cat)) {
			corpus.put(cat, new ArrayList<String>());
		}
		corpus.get(cat).add(twit);
	}

	private void treatTwitt(String strLine, int twittColumn) {
		
		String[] lis = strLine.split(",");
		String twit = lis[twittColumn].substring(1,
				lis[twittColumn].length() - 1);
		document.add(twit);
	}

	/**
	 * Training parser
	 * 
	 * @param trainerPath
	 *            path to the file containing data
	 * @param includeFirstLine
	 *            whether or not the first line is relevant
	 * @param twittColumn
	 *            column of the twitt text
	 * @param categoryColumn
	 *            column of the category
	 * @return all the texts sprted by category
	 */
	public static HashMap<String, ArrayList<String>> parseCorpus(
			String trainerPath, boolean includeFirstLine, int twittColumn,
			int categoryColumn) {

		TwittParser tp = new TwittParser(trainerPath, includeFirstLine,
				twittColumn, categoryColumn);
		return tp.corpus;
	}

	/**
	 * Test parser
	 * 
	 * @param corpusPath
	 *            path to the file containing data
	 * @param includeFirstLine
	 *            whether or not the first line is relevant
	 * @param twittColumn
	 *            column containing the twitt text
	 * @return all the texts in a list
	 */
	public static ArrayList<String> parseCorpus(String corpusPath,
			boolean includeFirstLine, int twittColumn) {

		TwittParser tp = new TwittParser(corpusPath, includeFirstLine,
				twittColumn);
		return tp.document;
	}

}
